<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="oldPassword"
          type="password"
          name="oldPassword"
          label="旧密码"
          placeholder="请输入旧密码"
          :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
          v-model="newPassword"
          type="password"
          name="userPassword"
          label="新密码"
          placeholder="请输入新密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
          v-model="checkPassword"
          type="password"
          name="userPassword"
          label="再次输入"
          placeholder="请再次输入新密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup lang="ts">
import myAxios from "../plugins/myAxios";
import {useRoute} from "vue-router";
import {ref} from "vue";
import {Toast} from "vant";

const route = useRoute();

const oldPassword = ref('');
const newPassword = ref('');
const checkPassword = ref('');

const onSubmit = async () => {
  // 可以增加一些前端校验
  console.log(newPassword.value);
  if (newPassword.value !== checkPassword.value) {
    Toast.fail("二次输入的密码不一致");
    return;
  }
  const res = await myAxios.post('/user/updatePwd', {
    oldPwd: oldPassword.value,
    newPwd : newPassword.value,
    checkPwd : checkPassword.value,
  })
  if (res.code === 0 && res.data) {
    // 跳转到之前的页面
    Toast.success("修改成功")
    window.location.href = route.query?.redirect as string ?? '/';
  } else {
    Toast.fail('修改失败'+res.description);
  }
};
</script>
<style scoped>

</style>